Skip to content

move late imports to top #26750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 11, 2019
Merged

move late imports to top #26750

merged 8 commits into from
Jun 11, 2019

Conversation

xcz011
Copy link
Contributor

@xcz011 xcz011 commented Jun 9, 2019

import numpy as np

from pandas.core.dtypes.generic import ABCSeries
from pandas.core.dtypes.missing import remove_na_arraylike
from pandas.core.reshape.concat import concat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import from pandas

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just updated it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback I still pretty new here. Can I ask why this place could directly use import from pandas not import as previously ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously before @datapythonista reorg (just merged)
the plotting code was basically imported during pandas init
and the pandas namespace was not fully defined yet

it’s now essentially decoupled from that so that it a lazy import
this we can use top of module imports rather than import into each function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciated for this detail explanation. This is helpful.

import numpy as np

from pandas.core.dtypes.generic import ABCSeries
from pandas.core.dtypes.missing import remove_na_arraylike

from pandas import concat
from pandas.core.series import Series
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Series as well

@datapythonista
Copy link
Member

@xcz011 probably worth just simply import pandas as pd at the beginning and then just prefix pd.Series and pd.concat. Also, I think you're doing it, but just in case, you'll have to run isort <the modified file> as the imports must be in a specific order.

Did you check if in the other files inside pandas/plotting/_matplotlib are there similar cases?

Thanks!

@xcz011
Copy link
Contributor Author

xcz011 commented Jun 9, 2019

@xcz011 probably worth just simply import pandas as pd at the beginning and then just prefix pd.Series and pd.concat. Also, I think you're doing it, but just in case, you'll have to run isort <the modified file> as the imports must be in a specific order.

Did you check if in the other files inside pandas/plotting/_matplotlib are there similar cases?

Thanks!

@datapythonista I found some lazy matplotlib imports in other files , such as pandas/plotting/_matplotlib/hist.py , do I need change those ? Thanks

@codecov
Copy link

codecov bot commented Jun 9, 2019

Codecov Report

Merging #26750 into master will increase coverage by 0.02%.
The diff coverage is 72.22%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #26750      +/-   ##
==========================================
+ Coverage    91.7%   91.72%   +0.02%     
==========================================
  Files         179      178       -1     
  Lines       50767    50774       +7     
==========================================
+ Hits        46555    46574      +19     
+ Misses       4212     4200      -12
Flag Coverage Δ
#multiple 90.32% <72.22%> (+0.03%) ⬆️
#single 41.19% <0%> (-0.1%) ⬇️
Impacted Files Coverage Δ
pandas/plotting/_matplotlib/tools.py 78.4% <100%> (ø) ⬆️
pandas/plotting/_matplotlib/timeseries.py 66.32% <100%> (ø) ⬆️
pandas/plotting/_matplotlib/style.py 85.96% <100%> (ø) ⬆️
pandas/plotting/_matplotlib/misc.py 38.93% <50%> (+0.9%) ⬆️
pandas/plotting/_matplotlib/boxplot.py 75.59% <66.66%> (+0.48%) ⬆️
pandas/io/gbq.py 78.94% <0%> (-10.53%) ⬇️
pandas/core/indexes/interval.py 96.11% <0%> (-0.32%) ⬇️
pandas/core/frame.py 96.88% <0%> (-0.12%) ⬇️
pandas/core/indexes/datetimes.py 96.37% <0%> (ø) ⬆️
pandas/core/indexes/timedeltas.py 90.96% <0%> (ø) ⬆️
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c7748ca...d95a9bf. Read the comment docs.

@codecov
Copy link

codecov bot commented Jun 9, 2019

Codecov Report

Merging #26750 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #26750      +/-   ##
==========================================
- Coverage    91.7%   91.69%   -0.01%     
==========================================
  Files         179      179              
  Lines       50767    50764       -3     
==========================================
- Hits        46555    46550       -5     
- Misses       4212     4214       +2
Flag Coverage Δ
#multiple 90.29% <100%> (ø) ⬆️
#single 41.2% <0%> (-0.09%) ⬇️
Impacted Files Coverage Δ
pandas/plotting/_matplotlib/boxplot.py 75.71% <100%> (+0.59%) ⬆️
pandas/io/gbq.py 78.94% <0%> (-10.53%) ⬇️
pandas/core/frame.py 96.88% <0%> (-0.12%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c7748ca...256a536. Read the comment docs.

@datapythonista
Copy link
Member

@xcz011 all those were lazy to avoid requiring matplotlib if plotting wasn't used. Now that is managed in pandas/plotting/_core.py::_get_plot_backend that doesn't load pandas/plotting/_matplotlib until the user plots something.

So, you can move the other imports to the top too, that's not required and should improve readability of the file.

@xcz011
Copy link
Contributor Author

xcz011 commented Jun 10, 2019

@xcz011 all those were lazy to avoid requiring matplotlib if plotting wasn't used. Now that is managed in pandas/plotting/_core.py::_get_plot_backend that doesn't load pandas/plotting/_matplotlib until the user plots something.

So, you can move the other imports to the top too, that's not required and should improve readability of the file.

@datapythonista Thanks for this detail explanation! I just made change to most of files in _matplotlib, except core.py. I not fully sure which do I need move those lazy import to the top too ? Thanks !

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, couple of comments. You can also move the ones in _core.py (the pandas and matplotlib ones, you can leave the rest where they are I think).

@@ -2,6 +2,7 @@

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scipy ones should be moved. It's the same case as with matplotlib, we don't require it unless you call a function that uses it. In this case, if you call a plot (like the line plot), you'll need matplotlib, but not scipy, which you only need if you call the kde plot.

This is why the tests are failing (not sure if you've seen the CI is in red). Can you restore the scipy ones please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Thanks!

@@ -1,6 +1,12 @@
# being a bit too dynamic
from math import pi, sqrt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete the comment, I guess it refers to how we were importing before. But doesn't seem to add much value in any case.

@jreback do you want to avoid loading math unless the user calls the andrews_curves plot, like it was? Or you're ok with this? same for random below

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change these to be np.pi and np.sqrt; we don't use math generally

don't have to load scipy unless user call kdeplot
@@ -1,6 +1,12 @@
# being a bit too dynamic
from math import pi, sqrt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change these to be np.pi and np.sqrt; we don't use math generally

@jreback
Copy link
Contributor

jreback commented Jun 11, 2019

looks fine, these are isorted by default right? @WillAyd

@jreback jreback added this to the 0.25.0 milestone Jun 11, 2019
@WillAyd
Copy link
Member

WillAyd commented Jun 11, 2019

isort doesn't make mods by default in our CI but would fail it if out of order, so this looks good

@WillAyd WillAyd merged commit deffee6 into pandas-dev:master Jun 11, 2019
@WillAyd
Copy link
Member

WillAyd commented Jun 11, 2019

Thanks @xcz011 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PLOT: Fix imports of plotting
5 participants